package com.vpx.session.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.vpx.server.framework.authority.SessionStoreManager;
import com.vpx.server.framework.base.ReplyJson;
import com.vpx.server.framework.base.SessionContext;
import com.vpx.server.bean.constant.HashMapSizeConstant;
import com.vpx.server.framework.authority.SessionInfo;
import com.vpx.server.framework.config.SystemConfig;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.framework.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xx
 * @title: vpx
 * @module 
 * @description: 
 * @date 
 */
@RestController
@RequestMapping("/api/session")
@Api(tags = "session")
public class SessionController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    private StringRedisTemplate stringRedisTemplate;

	@Autowired
    private RedisTemplate<String, String> redisTemplate;

	@Autowired
	SystemConfig systemConfig;

	/**
	 * 获取token
	 * @param request
	 */
	@GetMapping(value = "getToken")
	@ApiOperation("获取token")
	public ReplyJson getToken(HttpServletRequest request){
		try {

			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setSid(UUIDUtil.getUuid());
			sessionInfo.setCreatetime(DateUtils.getTime());
			sessionInfo.setLastTime(DateUtils.getTime());

			String key = SessionContext.getSessionRedisKey(sessionInfo.getSid());
			//session保存在本地
			SessionStoreManager.setSession(key, sessionInfo);
			Map<String, Object> retMap = new HashMap<String, Object>(HashMapSizeConstant.HASH_MAP_SIZE);
			retMap.put("sid", sessionInfo.getSid());
			
			return ReplyJson.success(retMap);

		}catch(Exception e){
			e.printStackTrace();
			logger.info("system error", e);
			return ReplyJson.systemError();
		}
		
	}

}